<?php

namespace App\Kuafu\User\Validate;

use App\Kuafu\Base\Validate\BaseValidate;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Model\UserRoleMapping;

class RoleValidate extends BaseValidate
{
    public static function addRoleValidate($data)
    {
        $rules = [
            'name'             => 'required|max:20',
            'status'           => 'nullable|integer|between:0,2',
            'permissionList.*' => 'required|integer'
        ];

        $messages = [
            'name.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_NAME],
            'name.max'      => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_NAME_LENGTH],

            'status.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_STATUS],
            'status.between'  => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_STATUS],

            'permissionList.*.integer'  => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_PERMISSION],
            'permissionList.*.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_PERMISSION],
        ];

        return self::validateCheck($data, $rules, $messages);
    }

    public static function doRoleValidate($data)
    {
        $rules = [
            'roleId' => 'required|integer',
            'name'   => 'required|max:20',

            'permissionList.*' => 'required|integer'
//            'status'  => 'required|integer|between:0,2',
        ];

        $messages = [
            'roleId.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_ROLE_ID],

            'name.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_NAME],
            'name.max'      => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_NAME_LENGTH],

            'permissionList.*.integer'  => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_PERMISSION],
            'permissionList.*.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_PERMISSION],
//
//            'status.required' => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_STATUS],
//            'status.between'  => UserCodeEnum::CODE_TEXT[UserCodeEnum::ERR_PMS_ROLE_PARAM_STATUS],
        ];

        return self::validateCheck($data, $rules, $messages);
    }

    /**
     * 校验当前角色是否有人再使用
     *
     * @param int $roleId
     */
    public static function isInUsedRole(int $roleId)
    {
        $count = UserRoleMapping::countRoleId($roleId);
        if ($count) {
            return true;
        }

        return false;
    }

}
